﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HNG.LIB.Controls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class AdminPortal_Controls_SanPham_ProductList : System.Web.UI.UserControl
{
    string LangID = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
    
    TblSanPhamManager _TblSanPhamManager = new TblSanPhamManager();
    public int PageSize
    {
        get { return Paging1.PageSize; }
        set { Paging1.PageSize = value; }
    }

    protected int _StartIndex;
    public int StartIndex
    {
        get { return _StartIndex; }
        set { _StartIndex = value; }
    }

    public bool Paging
    {
        get { return Paging1.Visible; }
        set { Paging1.Visible = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {            
            Paging1.CurrentPage = 1;
            FunctionExtend.LoadPageSize(ddlPageSize);
            LoadDataToGrid(new TblSanPhamEntity());
        }
    }

    private void LoadDataToGrid(TblSanPhamEntity _TblSanPhamEntity)
    {
        Paging1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
        _TblSanPhamEntity.LangId = LangID;
        _TblSanPhamEntity.StartIndex = (Paging1.CurrentPage - 1) * Paging1.PageSize;
        _TblSanPhamEntity.EndIndex = Paging1.CurrentPage * Paging1.PageSize;
        if (Paging1.CurrentPage > 1) _TblSanPhamEntity.StartIndex += 1;
        DataTable dt = _TblSanPhamManager.be_selectToGridView(_TblSanPhamEntity);
        if (dt != null)
        {
            if (dt.Rows.Count > 0)
            {
                Paging1.TotalItems = Convert.ToInt32(dt.Rows[0]["TotalItems"].ToString());
            }
            dgvListCategoryNews.DataSource = dt;
            dgvListCategoryNews.DataBind();
            lblTotalRecord.Text = dt.Rows.Count.ToString();
        }
        if (Paging1.Visible) Paging1.BeginPaging();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/AdminPortal/Pages/ProductAdd.aspx");
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in dgvListCategoryNews.Rows)
        {
            HtmlInputCheckBox chkID = (HtmlInputCheckBox)row.FindControl("chkID");
            
            if (chkID.Checked)
            {
                try
                {
                    TblSanPhamEntity _TblSanPhamEntity = _TblSanPhamManager.SelectOne(Convert.ToInt32(chkID.Value));
                    if (!string.IsNullOrEmpty(_TblSanPhamEntity.Image))
                    {
                        string strPath = Server.MapPath(_TblSanPhamEntity.Image);
                        if (File.Exists(strPath)) File.Delete(strPath);
                    }
                    
                    //delete anh di kem 
                    DeletePriductImagesub(Convert.ToInt32(chkID.Value));
                    new TblHinhAnhSanPhamManager().DeleteByIdsanPham(Convert.ToInt32(chkID.Value));

                    _TblSanPhamManager.DeleteById(Convert.ToInt32(chkID.Value));
                }
                catch { }
            }
        }
        Paging1.CurrentPage = 1;
        LoadDataToGrid(new TblSanPhamEntity());
    }

    private void DeletePriductImagesub(int PID)
    {
        DataTable _dtimg = new TblHinhAnhSanPhamManager().SelectByIdsanPhamRDT(PID);

        if (_dtimg != null && _dtimg.Rows.Count > 0)
        {
            foreach (DataRow r in _dtimg.Rows)
            {
                if (!string.IsNullOrEmpty(r[TblHinhAnhSanPhamFields.Image.Name].ToString()))
                {
                    string filePath = Server.MapPath(r[TblHinhAnhSanPhamFields.Image.Name].ToString());
                    try
                    {
                        File.Delete(filePath);
                    }
                    catch
                    {
                    }
                }
            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        TblSanPhamEntity _TblSanPhamEntity = new TblSanPhamEntity();
        _TblSanPhamEntity.Condition = txtKeySearch.Text;
        _TblSanPhamEntity.LangId = LangID;
        Paging1.CurrentPage = 1;
        LoadDataToGrid(_TblSanPhamEntity);

    }
    protected void dgvListCategoryNews_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView r = (DataRowView)e.Row.DataItem;

        HyperLink lnkTitle = (HyperLink)e.Row.FindControl("lnkTitle");

        Image imgUpdate = (Image)e.Row.FindControl("imgUpdate");

        ThumbnailImages ThumbnailImages1 = (ThumbnailImages)e.Row.FindControl("ThumbnailImages1");

        if (r != null && lnkTitle != null && imgUpdate != null)
        {
            lnkTitle.Text = r[TblSanPhamFields.TenSanPham.Name].ToString();
            lnkTitle.NavigateUrl = "~/AdminPortal/Pages/ProductEdit.aspx?id=" + r[TblSanPhamFields.Id.Name].ToString();

            if (r[TblSanPhamFields.TrangThai.Name].ToString().Equals("True"))
                imgUpdate.ImageUrl = "~/AdminPortal/Images/Icon/tree_dnd_yes.png";
            else
                imgUpdate.ImageUrl = "~/AdminPortal/Images/Icon/tree_dnd_no.png";
            imgUpdate.Attributes.Add("onclick", "javascript:changeStatusProduct('" + imgUpdate.ClientID + "','" + r[TblSanPhamFields.Id.Name].ToString() + "')");

            ThumbnailImages1.ImageUrl = r[TblSanPhamFields.Image.Name].ToString();
        }
    }
    protected void Paging1_OnPageIndexChanged(object sender, EventArgs e)
    {
        LoadDataToGrid(new TblSanPhamEntity());
        Paging1.BeginPaging();
    }
    protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        Paging1.CurrentPage = 1;
        LoadDataToGrid(new TblSanPhamEntity());
    }
    protected void dgvListCategoryNews_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "ListProductByCate")
        {
            TblSanPhamEntity _TblSanPhamEntity = new TblSanPhamEntity();
            _TblSanPhamEntity.DM = e.CommandArgument.ToString();
            LoadDataByCateID(_TblSanPhamEntity);
            Paging1.Visible = ddlPageSize.Visible = false;

        }
    }
    //dung cho dgvListCategoryNews_RowCommand khong phan trang
    private void LoadDataByCateID(TblSanPhamEntity _TblSanPhamEntity)
    {
        _TblSanPhamEntity.LangId = LangID;
        DataTable dt = _TblSanPhamManager.be_selectToGridView(_TblSanPhamEntity);
        if (dt != null)
        {
            dgvListCategoryNews.DataSource = dt;
            dgvListCategoryNews.DataBind();
            lblTotalRecord.Text = dt.Rows[0]["TotalItems"].ToString();
        }
    }
}